/**
 * Created by IRronChen on 2016/6/20.
 */

var singleDetailePageManager = new (function(){
    var _isWifi = false;
    var _isAutoPlay = false;
    var _isVideoHandlerInit = false;
    var _confirmPlay = false;
    var oVideo = document.getElementById('videoId');
    this.fillCalenderInfo = function(sDate)
    {
        $("#toCalender span").text(sDate);
    }
    this.fillPositionInfo = function(sPosition)
    {
        $(".toMap span").text(sPosition);
    }
    this.callMeToGetLikeList = function(strJson)
    {
        var oJson = JSON.parse(strJson);
        var option = oJson.data.items;
        var jqContainer = $(".headPortraits ul");
        $(".like_user").remove();
        for(var i=0;i<option.length;i++)
        {
            var jqLi = $("<li class='headPortraits_li like_user'>");
            jqLi.append($("<img src='"+option[i].iconUrl+"' class='face_radius'>"));
            jqContainer.append(jqLi);
        }
        $(".personNum dd").text(option.length+"人喜欢");
    }

    this.callVideoInit = function(option)
    {
        $(".play-alert a").click(function(){
            _confirmPlay = true;
            oVideo.play();
            $(".play-alert").hide();
        });
        _isWifi = !!option.isWifi;
        _isAutoPlay = !!option.isAutoPlay;
        _isVideoHandlerInit = true;
        if(_isWifi && _isAutoPlay)
        {
            oVideo.play();
        }
    }
    this.handleVideoLoading = function(str)
    {
       $(".play-alert.loading-alert").text(str).show();
    }
    this.handleVideoAlert = function()
    {
    //   $(".play-alert.loading-alert").show();
        if(_confirmPlay || _isWifi || !_isVideoHandlerInit)
        {
            return;
        }
        if(!_isWifi)
        {
            oVideo.pause();
            $(".play-alert.network-alert").show();
        }

    }
    this.handlePlayStart = function()
    {
    //    $(".play-alert.loading-alert").show();

        if($(".play-alert.network-alert").css("display")!="none")
        {
            $(".play-alert.network-alert").hide();
            _confirmPlay = true;
        }
    }
})();
(function(){
    var initPage = function()
    {
        var jqEndTime = $("#time");
        if(!!jqEndTime.attr("alt")){
            jqEndTime.text();
        }

        $("#toCalender").click(function(){ //价格日历 javascript:singleDetailePageManager.fillCalenderInfo("2015-01-01")
            myObj.fireEventToApp("selectCalender");//toSelectCalenderFromApp();

        });
        $(".lookmore").click(function(){ //更多评论
            if($(this).text()=="查看更多")
            {
                myObj.fireEventToApp("moreComment");
            }
            // myObj.toMoreCommentAtApp();
        });
        $(".toMap").click(function(){ //地图 javascript:singleDetailePageManager.fillPositionInfo("鼓浪屿")
            myObj.fireEventToApp("toMap" );

        });
        $(".headPortraits").click(function(){ //喜欢->更多
            myObj.fireEventToApp("likeList");
        });
        $(".leader").click(function(){ //领队
            myObj.fireEventToApp("leaderDetail", $(this).attr("alt"));
        });
        $(".lookall").click(function(){
            var jqContainer = $(".main_content_text");
            if(jqContainer.hasClass("main_content_open"))
            {
                jqContainer.removeClass("main_content_open").addClass("main_content_fold");
                $(this).toggleClass("lookall_open lookall_fold").find("span").text("查看全部");
            }
            else if(jqContainer.hasClass("main_content_fold"))
            {
                jqContainer.removeClass("main_content_fold").addClass("main_content_open");
                $(this).toggleClass("lookall_open lookall_fold").find("span").text("收起");
            }
        });
        $(".video_cover").click(function(){
            var jqVideo = $("#videoId")
            myObj.h5VideoPlay(jqVideo.attr("src"), $("title").first().text(), jqVideo.attr("poster"));
        })
    };

    var initPageFromShare = function()
    {
        $(".from_share").show();
        $(".lookall").click(function(){
            var jqContainer = $(".main_content_text");
            if(jqContainer.hasClass("main_content_open"))
            {
                jqContainer.removeClass("main_content_open").addClass("main_content_fold");
                $(this).toggleClass("lookall_open lookall_fold").find("span").text("查看全部");
            }
            else if(jqContainer.hasClass("main_content_fold"))
            {
                jqContainer.removeClass("main_content_fold").addClass("main_content_open");
                $(this).toggleClass("lookall_open lookall_fold").find("span").text("收起");
            }
        });
        $(".toCalendar label").removeClass("icon_right");
        $(".headPortraits").remove();
        $(".comments").remove();

        var ifrSrc = 'larkapp://platformapi/startApp?shareType=outdoor&activityId='+$("#activityId").val()+'&activityName='+$("title").first().text();
        var url = 'https://callapp.quncaotech.com/h5/toDownload?shareType=outdoor&activityId='+$("#activityId").val()+'&activityName='+$("title").first().text();
        $(".open_app").click(function(){
            var ver = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
            if(ver && parseInt(ver[1]) >=9)
            {
                location.href = url;
            }
            else
            {
                var ifr = document.createElement('iframe');
                ifr.src = ifrSrc;
                ifr.style.display = 'none';
                document.body.appendChild(ifr);
                setTimeout(function() {
                    document.body.removeChild(ifr);
                    location.href = url;
                }, 800);
            }
        });
        $(".download-closed").click(function(){
            $(".na-download").hide();
        });
        var oVideo = document.getElementById('videoId');
        $(".video_cover").click(function(){
            $(this).hide();
            oVideo.play();
        })
    }

    var jqBanner = $(".ac_banner");
    console.log(jqBanner.width()+","+ jqBanner.height()+","+ $(window).width());
    if(jqBanner)
    {
        if((jqBanner.width()/jqBanner.height()) < (16/9))
        {
            jqBanner.parent().height($(window).width()/16*9);
        }
        else if((jqBanner.width()/jqBanner.height()) > (16/9))
        {
            jqBanner.parent().height($(window).width()/16*9);
            jqBanner.css({
                width:"",
                height: "100%"
            })
        }
    }

    var nowTime = new Date().getTime();
    $(".comment-time").each(function(){
        var commentTime = new Date(parseInt($(this).attr("alt"))).getTime();
        var dif = nowTime - commentTime;
        if(dif < 3600000)
        {
            var iMin = parseInt(dif/60000);
            $(this).text(iMin + "分钟前");
        }
        else if(dif >= 3600000 && dif < 3600000*24 )
        {
            var iH = parseInt(dif/3600000);
            $(this).text(iH + "小时前");
        }
        else if(dif >= 3600000*24 && dif <  3600000*24*7)
        {
            var iDay = parseInt(dif/86400000);
            $(this).text(iDay + "天前");
        }
        else
        {
            var commentDate = new Date(parseInt($(this).attr("alt")));
            $(this).text(commentDate.getMonth() + "-" + commentDate.getDate());
        }
    });
    var from = getUrlParam("from");
    if(from == "share" || from == "singlemessage")
    {
        initPageFromShare();
    }
    else
    {
        initPage();
    }

    myObj.fireEventToApp("callVideoInit" );

})();
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
